<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>深浅拷贝2</title>
</head>

<body>
    <script>
        var father = {name:'hjx', age:21, job:['student', 'internStudent']};
        
        // 浅拷贝
        var child = {};
        for(var key in father) {
            child[key] = father[key];
        }
        
        console.log('================浅拷贝===============');
        console.log('-----------father-----------');
        console.log(father);
        console.log('---------- child------------');
        console.log(child);

        // 修改值类型(互不干扰)
        child.name = 'hxh';
        console.log('----修改值类型-------father-----------');
        console.log(father);
        console.log('---------- child------------')
        console.log(child);

        // 修改引用类型(互相影响)
        child.job[0] = '学生';
        child.name = 'hxh';
        console.log('----修改引用类型-------father-----------');
        console.log(father);
        console.log('---------- child------------');
        console.log(child);

        // 总结：使用浅拷贝得到的对象，修改值类型两者互不干扰，修改引用类型则互相影响

        // 深拷贝
        // 方法一
        console.log('=================深拷贝=================');
        console.log('=================方法一=================');
        father = {name:'hjx', age:21, job:['student', 'internStudent']};
        child = {};
        function deepCopy(obj) {
            var o;
            switch(typeof obj) {
                case 'string':
                    o = obj + '';
                    break;
                case 'number':
                    o = obj - 0;
                    break;
                case 'boolean':
                    o = obj;
                    break;
                case 'undefined':
                    break;
                case 'object':
                    if(obj === null) {
                        o = null;
                    }else {
                        if(obj instanceof Array) {
                            o = [];
                            for(var i = 0, len = obj.length; i < len; i++) {
                                o.push(deepCopy(obj[i]));
                            }
                        }else {
                            o = {};
                            for(var key in obj) {
                                o[key] = deepCopy(obj[key]);
                            }
                        }
                    }
                    break;
                default:
                    o = obj;
                    break;
            }
            return o;
        }

        child = deepCopy(father);
        console.log('---深拷贝--------father-----------');
        console.log(father);
        console.log('---------- child------------');
        console.log(child);
        // 修改值类型
        child.name = 'hxh';
        console.log('---修改值类型--------father-----------');
        console.log(father);
        console.log('---------- child------------');
        console.log(child);
        // 修改引用类型
        child.job[0] = '学生';
        console.log('---修改引用类型--------father-----------');
        console.log(father);
        console.log('---------- child------------');
        console.log(child);
        console.log('---------------------------------------');


        // 方法二：最简单的
        console.log('=================方法二=================');
        father = {name:'hjx', age:21, job:['student', 'internStudent']};
        function deepCopy2(obj) {
            console.log(obj);
            var str = JSON.stringify(obj); 
            // stringify的学习
            // https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
            // obj = {name: "hjx", age: 21, job: Array(2)}
            // JSON.stringify() 将对象转换为字符串，{"name":"hjx","age":21,"job":["student","internStudent"]}
            // JSON.parse() 方法用于将一个 JSON 字符串转换为对象。{name: "hjx", age: 21, job: Array(2)}
            console.log('        ---->' + str);
            var str2 = JSON.parse(str);
            return JSON.parse(JSON.stringify(obj));
        }
        console.log(deepCopy2(father));
        child = deepCopy(father);
        console.log(child);

        // 修改值类型
        child.name = 'hxh';
        console.log('---修改值类型--------father-----------');
        console.log(father);
        console.log('---------- child------------');
        console.log(child);

        // 修改引用类型
        child.job[0] = '学生';
        child.job.push('yixun');
        console.log('---修改引用类型--------father-----------');
        console.log(father);
        console.log('---------- child------------');
        console.log(child);
        console.log('---------------------------------------');

        
    </script>
</body>

</html>